<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="创建者" prop="createBy">
              <el-input
                  v-model="queryParams.createBy"
                  placeholder="请输入创建者"
                  clearable
                  @keyup.enter="handleQuery"
              />
            </el-form-item>-->
      <!--      <el-form-item label="创建时间" prop="createTime">
              <el-date-picker clearable
                              v-model="queryParams.createTime"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择创建时间">
              </el-date-picker>
            </el-form-item>-->
      <!--      <el-form-item label="更新者" prop="updateBy">
              <el-input
                  v-model="queryParams.updateBy"
                  placeholder="请输入更新者"
                  clearable
                  @keyup.enter="handleQuery"
              />
            </el-form-item>-->
      <!--      <el-form-item label="更新时间" prop="updateTime">
              <el-date-picker clearable
                              v-model="queryParams.updateTime"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择更新时间">
              </el-date-picker>
            </el-form-item>-->
      <!--      <el-form-item label="归属人" prop="userId">
              <el-input
                  v-model="queryParams.userId"
                  placeholder="请输入归属人"
                  clearable
                  @keyup.enter="handleQuery"
              />
            </el-form-item>-->
      <el-form-item label="标题" prop="title">
        <el-input
            v-model="queryParams.title"
            placeholder="请输入标题"
            clearable
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="未启用" value="0"></el-option>
          <el-option label="启用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="置顶" prop="Top">
              <el-input
                  v-model="queryParams.Top"
                  placeholder="请输入置顶"
                  clearable
                  @keyup.enter="handleQuery"
              />
            </el-form-item>-->
      <!--      <el-form-item label="阅读量" prop="views">
              <el-input
                  v-model="queryParams.views"
                  placeholder="请输入阅读量"
                  clearable
                  @keyup.enter="handleQuery"
              />
            </el-form-item>-->
      <!--      <el-form-item label="首页图片" prop="blogPic">
              <el-input
                  v-model="queryParams.blogPic"
                  placeholder="请输入首页图片"
                  clearable
                  @keyup.enter="handleQuery"
              />
            </el-form-item>-->
      <!--      <el-form-item label="简介" prop="blogDesc">
              <el-input
                  v-model="queryParams.blogDesc"
                  placeholder="请输入简介"
                  clearable
                  @keyup.enter="handleQuery"
              />
            </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['cms:blog:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['cms:blog:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['cms:blog:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['cms:blog:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="blogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>

      <el-table-column label="首页图片" align="center" prop="blogPic">
        <!--        <template #default="scope">
                  <el-image
                      :src="scope.row.blogPic"
                      width="100"
                      height="100"
                  ></el-image>
                </template>-->
      </el-table-column>

      <!--      <el-table-column label="ID" align="center" prop="id"/>-->
      <el-table-column label="创建者" align="center" prop="createBy"/>
      <!--      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
              <template #default="scope">
                <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>-->
      <el-table-column label="更新者" align="center" prop="updateBy"/>
      <!--      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
              <template #default="scope">
                <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>-->
      <!--      <el-table-column label="归属人" align="center" prop="userId" />-->
      <el-table-column label="标题" align="center" prop="title"/>
      <el-table-column label="类型" align="center" prop="type"/>
      <!--      <el-table-column label="内容" align="center" prop="content" />-->
      <!--      <el-table-column label="置顶" align="center" prop="Top" />-->
      <el-table-column label="阅读量" align="center" prop="views"/>
      <el-table-column label="状态" align="center" prop="status">
        <!--        <template slot-scope="scope">
                  {{ scope.row.status === 1 ? '启用' : '禁用' }}
                </template>-->
      </el-table-column>
      >
      <!--          <template #default="scope">
                  <span v-if="scope.row.status === 0">禁用</span>
                  <span v-else-if="scope.row.status === 1">启用</span>
                </template>-->

      <el-table-column label="简介" align="center" prop="blogDesc"/>
      <!--      <el-table-column label="附件列表" align="center" prop="blogFiles" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['cms:blog:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['cms:blog:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改博客信息对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="blogRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="首页图片" prop="blogPic">
              <imageUpload v-model="form.blogPic" :limit="1"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="置顶" prop="Top"/>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容">
              <editor v-model="form.content" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Blog">
import {listBlog, getBlog, delBlog, addBlog, updateBlog} from "@/api/cms/blog";
const contentRef = ref("");
const contentComputed = computed({
  get: () => form.content,
  set: (value) => (contentRef.value = value),
});
watch(contentRef, (newValue) => (form.content = newValue));
const {proxy} = getCurrentInstance();
const blogList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const editorOptions = ref({
  modules: {
    toolbar: [
      ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
      ['blockquote', 'code-block'],
      [{'header': 1}, {'header': 2}],               // custom button values
      [{'list': 'ordered'}, {'list': 'bullet'}],
      [{'script': 'sub'}, {'script': 'super'}],      // superscript/subscript
      [{'indent': '-1'}, {'indent': '+1'}],          // outdent/indent
      [{'direction': 'rtl'}],                         // text direction

      [{'size': ['small', false, 'large', 'huge']}],  // custom dropdown
      [{'header': [1, 2, 3, 4, 5, 6, false]}],
      [{'color': []}, {'background': []}],          // dropdown with defaults from theme
      [{'font': []}],
      [{'align': []}],

      ['clean'],                                        // remove formatting button

      ['link', 'image', 'video']                        // link and image, video
    ]
  },
  theme: 'snow'
});


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    userId: null,
    title: null,
    type: null,
    //content: null,
    Top: null,
    views: null,
    status: null,
    blogPic: null,
    blogDesc: null,
    blogFiles: null
  },
  rules: {
    title: [
      {required: true, message: "标题不能为空", trigger: "blur"}
    ],
    type: [
      {required: true, message: "类型不能为空", trigger: "change"}
    ],

  },

});

const {queryParams, form, rules} = toRefs(data);

/** 查询博客信息列表 */
function getList() {
  loading.value = true;
  listBlog(queryParams.value).then(response => {
    blogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
  console.log(blogList)
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    userId: null,
    title: null,
    type: null,
    content: undefined,
    Top: null,
    views: null,
    status: null,
    blogPic: null,
    blogDesc: null,
    blogFiles: null
  };
  proxy.resetForm("blogRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}


/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加博客信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBlog(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改博客信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["blogRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        console.log(form.value)
        updateBlog(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBlog(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除博客信息编号为"' + _ids + '"的数据项？').then(function () {
    return delBlog(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('cms/blog/export', {
    ...queryParams.value
  }, `blog_${new Date().getTime()}.xlsx`)
}

getList();
</script>
